package org.basis.algorithm.list.practice;

import java.util.TreeMap;

/**
 * treeMap练习
 *
 * @author Mr_wenpan@163.com 2021/12/08 16:40
 */
public class TreeMapPractice {

    public static void main(String[] args) {
        // treeMap按照key有序组织，增删改查时间复杂度为O（logN），底层实现是红黑树或跳表或AVL树
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(5, "5");
        treeMap.put(9, "9");
        treeMap.put(1, "1");
        treeMap.put(0, "0");
        treeMap.put(11, "11");
        treeMap.put(2, "2");

        System.out.println("treeMap.get(5) = " + treeMap.get(5));
        System.out.println("treeMap.firstEntry() = " + treeMap.firstEntry());
        System.out.println("treeMap.firstKey() = " + treeMap.firstKey());
        System.out.println("treeMap.lastEntry() =" + treeMap.lastEntry());
        System.out.println("treeMap.lastKey() = " + treeMap.lastKey());
        System.out.println("treeMap.pollFirstEntry() = " + treeMap.pollFirstEntry());
        System.out.println("treeMap.higherKey(7) = " + treeMap.higherKey(7));
        System.out.println("treeMap.higherEntry(7) = " + treeMap.higherEntry(7));
        System.out.println("treeMap.lowerKey(7) = " + treeMap.lowerKey(7));
        System.out.println("treeMap.lowerEntry(7) = " + treeMap.lowerEntry(7));

        System.out.println("treeMap.ceilingKey(5) = " + treeMap.ceilingKey(5));
        System.out.println("treeMap.ceilingEntry(5) = " + treeMap.ceilingEntry(5));

        System.out.println("treeMap.floorKey(5) = " + treeMap.floorKey(5));
        System.out.println("treeMap.floorEntry(5) = " + treeMap.floorEntry(5));

        treeMap.remove(5);

        System.out.println("=============================================================================");

        // 如果表中存入过key，返回key;否则返回所有键值的排序结果中， key的后一个
        System.out.println("treeMap.ceilingKey(5) = " + treeMap.ceilingKey(5));
        System.out.println("treeMap.ceilingEntry(5) = " + treeMap.ceilingEntry(5));

        // 如果表中存入过key，返回key;否则返回所有键值的排序结果中， key的前一个
        System.out.println("treeMap.floorKey(5) = " + treeMap.floorKey(5));
        System.out.println("treeMap.floorEntry(5) = " + treeMap.floorEntry(5));

    }
}
